package hongcheng.code_generator;

import java.io.File;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.Test;

import cn.hutool.extra.template.engine.freemarker.SimpleStringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import hongcheng.code_generator.config.DefaultXMLConfigReader;
import hongcheng.code_generator.database.DefaultMySqlDBMetadataReader;
import hongcheng.code_generator.entity.config.CodeGeneratorConfig;
import hongcheng.code_generator.entity.db.TableMetadata;

public class TTTT {

	@Test
	public void tteeet() throws Exception {
//		String xmlConfigFilePath = "D:\\workspace\\java_web\\code-generator\\NewFile.xml";
//		//1、读取代码生成配置XML文件
//		CodeGeneratorConfig codeGeneratorConfig = XMLConfigReader.build(xmlConfigFilePath).getCodeGeneratorConfig();
//		//2、读取数据库元数据
//		TableMetadata dbMetadata = DBMetadataReader.build(codeGeneratorConfig).getDBMetadata();
//		
//		Configuration cfg = new Configuration(Configuration.VERSION_2_3_29);
////		String dir = "D:\\workspace\\java_web\\code-generator\\src\\main\\resources\\template\\dao";
////		String dir = "D:\\workspace\\java_web\\code-generator\\src\\main\\resources\\template\\entity";
////		String dir = "D:\\workspace\\java_web\\code-generator\\src\\main\\resources\\template\\controller";
////		String dir = "D:\\workspace\\java_web\\code-generator\\src\\main\\resources\\template\\service";
////		String dir = "D:\\workspace\\java_web\\code-generator\\src\\main\\resources\\template\\mapper\\mysql";
//		String dir = "D:\\workspace\\java_web\\code-generator\\src\\main\\resources\\template\\vo";
////		cfg.setDirectoryForTemplateLoading(new File(dir));
//		cfg.setDefaultEncoding("UTF-8");
//		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
//		
////		String fileName = "entity.ftl";
////		String fileName = "dao.ftl";
////		String fileName = "controller.ftl";
////		String fileName = "service.ftl";
////		String fileName = "serviceImpl.ftl";
////		String fileName = "mysqlMapper.ftl";
//		String fileName = "D:\\workspace\\java_web\\code-generator\\src\\main\\resources\\template\\vo\\queryVo.ftl";
//		cfg.setTemplateLoader(new SimpleStringTemplateLoader());
//		cfg.setLocalizedLookup(false);
//		Template temp = cfg.getTemplate(fileName);
//	
//		Writer out = new OutputStreamWriter(System.out);
//		
//		Set<String> javaClassSet = dbMetadata.getTableColumnList().parallelStream().map((tableColumnMetadata)->{
//			return tableColumnMetadata.getJavaClassName();
//		}).collect(Collectors.toSet());
//		LinkedHashMap<String,Object> map = new LinkedHashMap<String,Object>();
//		map.put("package", "hongcheng.code_generator");
//		map.put("entityClassName", "CityDO");
//		map.put("voClassName", "CityQueryVO");
//		map.put("daoClassName", "CityMapper");
//		map.put("serviceClassName", "CityService");
//		map.put("controllerClassName", "CityController");
//		map.put("author", "hongcheng");
//		map.put("date", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//		map.put("table", dbMetadata);
//		map.put("javaClassSet", javaClassSet);
//		
//		
//		temp.process(map, out);
	}
}
